/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package services;

import Entitys.Fabricante;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

/**
 *
 * @author MAURO
 */
public class FabricanteServices {
    EntityManagerFactory emf= Persistence.createEntityManagerFactory("DrugStoreProPU");
         EntityManager em=emf.createEntityManager();
         
         public void create (Fabricante f){
          em.getTransaction().begin();
          em.persist(f);
          em.getTransaction().commit();
          em.close();
         }
         
         //Se añadio la funcionalidad con la base de datos
    
         public void delete (Integer   p){
          em.getTransaction().begin();
          Fabricante p1=em.find(Fabricante.class, p);
          if(p1!= null)
          em.remove(p1);
          em.getTransaction().commit();
          em.close();
         }
         
         public List<Fabricante> readAll(){
           Query q= em.createNamedQuery("Fabricante.findAll");
           return q.getResultList();
         }
         
         //this method return the Vendor's name  of the product corresponding wtih id

          public String readVendorName(Long id){
           Query q= em.createQuery("SELECT f.nombreFabricante FROM ProductoGeneral p, Fabricante f WHERE p.idFabricante=f.idFabricante AND p.idProducto=:idpro");
           q.setParameter("idpro", id);
           return (String) q.getSingleResult();
         }
}

                 
